import { defineStore } from 'pinia'
// import {  watch,computed } from 'vue'
import { articleGetCounts, articleGetList, articleGetListByAuth, articleGetById, articleGetTitleByType, articlePull, articleEdit, articleLog } from '../../api/article/index'
interface State {
  //文章数目
  articleCounts: number,
  //列表
  articleList: {
    page_all_num: number,
    list: Article[]
  },
  //文章
  article_now: Article,

  //文章标题列表
  articleTitlesList: ArticleTitle[],

  //修改文章
  articleEdit: {
    "article_id": string,
    "title": string,
    "tags": string,
    "type": string,
    "body": string,
    "cover_image": string
  },

  //保存文章
  articleSave: {
    "article_id": string,
    "title": string,
    "tags": string,
    "type": string,
    "body": string,
    "cover_image": string
  },

  //文章日志
  articleLogs: ArticleLogs[],

  //删除文章
  delArticleId: string,

  //目录
  articleCatalogs:articleCatalog[]

}
interface ArticleTitle {
  "article_id": string,
  "title": string,
}
interface ArticleLogs {
  "article_id": string,
  "title": string,
  "create_time": string,
  "body": string,
  "cover_image": string

}
interface Article {
  "article_id": string,
  "title": string,
  "tags": string,
  "type": string,
  "body": string,
  "views": number,
  "cover_image": string,
  "create_time": string,
  "comments_counts": number,
  "username": string
}
interface articleCatalog {
text:string, 
level: number
}

export const ArticleStore = defineStore('article', {
  //属性
  state: (): State => {
    return {
      articleCounts: 0,

      articleList: {
        page_all_num: 0,
        list: []
      },
      article_now: {
        article_id: "",
        title: "",
        tags: "",
        type: "",
        body: "",
        views: 0,
        cover_image: "noChange",
        create_time: "",
        comments_counts: 0,
        username: "passlink"
      },
      articleTitlesList: [],

      articleEdit: {
        article_id: "",
        title: "",
        tags: "",
        type: "",
        body: "",
        cover_image: "noChange"
      },

      articleLogs: [],
      delArticleId: '',
      articleSave: {
        article_id: "",
        title: "",
        tags: "",
        type: "",
        body: "",
        cover_image: ""
      },
      articleCatalogs:[]

    }
  },
  //数据处理
  getters: {


  },
  //函数
  actions: {

    //获取文章数
    async getCounts() {
      try {
        let res: any = await articleGetCounts();
        //赋值给存储
        this.articleCounts = res.data.data
      }
      catch (error) {
        console.log("获取数目失败")
        return
      }

    },
    //获取文章列表
    async getArticles(paramData: any) {
      try {
        let res: any = await articleGetList(paramData);
        //赋值给存储
        this.articleList = res.data.data

      }
      catch (error) {
        console.log("获取列表失败")
        return
      }

    },
    //获取文章列表通过作者
    async getArticlesByUser() {
      try {
        let res: any = await articleGetListByAuth();
        //赋值给存储
        this.articleList = res.data.data
      }
      catch (error) {
        console.log("获取列表失败")
        return
      }

    },

    //获取文章
    async getArticleBody(article_id: string) {
      //清空
      this.articleEdit = {
        article_id: "",
        title: "",
        tags: "",
        type: "",
        body: "",
        cover_image: "passlink"
      }
      try {
        let res: any = await articleGetById({ "article_id": article_id });
        //赋值给存储
  
      
        this.article_now = res.data.data
      }
      catch (error) {

        console.log("获取文章失败")
        return
      }

    },
    //获取文章日志
    async getArticlesLogs() {

      try {
        let res: any = await articleLog();
        //赋值给存储
        this.articleLogs = res.data.data.list
      }
      catch (error) {
        console.log("获取列表失败")
        return
      }

    },
    //上传文章
    async pullArticle() {
      try {
        await articlePull(this.articleEdit);
      }
      catch (error) {
       
        console.log("上传失败！！！")
        return
      }

    },

    //修改文章
    async editArticle() {
      try {
       await articleEdit(this.articleEdit);
        
      }
      catch (error) {
        console.log("上传失败！！！")
        return
      }

    },
    //获取文章标题列表
    async getTitleByType(paramData: any) {
      try {
        let res: any = await articleGetTitleByType(paramData);
        //赋值给存储
        this.articleTitlesList = res.data.data.list
      }
      catch (error) {

        console.log("获取相关文章失败")
        return
      }
    }
  },

  // 开启数据缓存
  persist: {
    enabled: true,
    strategies: [
      {
        // 自定义名称
        key: 'article',
        // 保存位置，默认保存在sessionStorage
        storage: localStorage,
        // 指定要持久化的数据，默认所有 state 都会进行缓存，你可以通过 paths 指定要持久化的字段，其他的则不会进行持久化。
        paths: ["articleSave", "article_now", "articleEdit", "articleCounts"],
      },
    ],
  },

})